﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

/// <summary>
/// MailUtility 的摘要描述
/// </summary>
public class MailUtility
{

  /// <summary>
  /// 使用文字傳送HTML MAIL
  /// </summary>
  /// <param name="mailfrom">寄件者</param>
  /// <param name="mailto">收件者</param>
  /// <param name="mailSubject">標題</param>
  /// <param name="html">內容</param>
  public static void SendHTMLMail(string mailfrom, string mailto, string mailSubject, string html)
  {
    try
    { 
      MailMessage mail = new MailMessage();
      mail.From = new MailAddress(ConfigurationManager.AppSettings["SMTP_from_address"].ToString(),
        ConfigurationManager.AppSettings["SMTP_from_name"].ToString(),
        Encoding.UTF8);
      mail.To.Add(new MailAddress(mailto));
      mail.IsBodyHtml = true;
      mail.Subject = mailSubject;
      mail.BodyEncoding = Encoding.UTF8;
      mail.Body = html;     
      SmtpClient smtpClient = new SmtpClient(); //從WEB.CONFIG讀SMTP CONFIG
      smtpClient.Send(mail);
    }
    catch (Exception ex)
    {
      throw ex;
    }
  }

  /// <summary>
  /// 使用控制項傳送HTML MAIL給單一使用者
  /// </summary>
  /// <param name="mailfrom">寄件者</param>
  /// <param name="mailto">收件者</param>
  /// <param name="mailSubject">標題</param>
  /// <param name="html">控制項</param>
  public static void SendHTMLMail(string mailfrom, string mailto, string mailSubject, Panel panel)
  {
    try
    {
      MailMessage mail = new MailMessage();
      mail.From = new MailAddress(ConfigurationManager.AppSettings["SMTP_from_address"].ToString(),
        ConfigurationManager.AppSettings["SMTP_from_name"].ToString(),
        Encoding.UTF8);
      mail.To.Add(new MailAddress(mailto));

      mail.IsBodyHtml = true;
      mail.Subject = mailSubject;
      mail.BodyEncoding = Encoding.UTF8;
      StringWriter sw = new StringWriter();
      HtmlTextWriter hw = new HtmlTextWriter(sw);
      panel.RenderControl(hw);
      mail.Body = sw.ToString();
      //SmtpClient smtpClient = new SmtpClient("192.168.1.20", 25);//設定E-mail Server和port
      SmtpClient smtpClient = new SmtpClient(); //從WEB.CONFIG讀SMTP CONFIG
      smtpClient.Send(mail);
    }
    catch (Exception ex)
    {
      throw ex;
    }
  }



  /// <summary>
  /// 使用控制項傳送HTML MAIL給多使用者
  /// </summary>
  /// <param name="mailfrom">寄件者</param>
  /// <param name="mailto">收件者陣列</param>
  /// <param name="mailcc">副件收件者陣列</param>
  /// <param name="mailbcc">密件收件者陣列</param>
  /// <param name="mailSubject">標題</param>
  /// <param name="panel">控制項</param> 
  public static void SendHTMLMail(string mailfrom, string[] mailto, string[] mailcc, string[] mailbcc, string mailSubject, Panel panel)
  {
    try
    {
      MailMessage mail = new MailMessage();
      mail.From = new MailAddress(ConfigurationManager.AppSettings["SMTP_from_address"].ToString(),
        ConfigurationManager.AppSettings["SMTP_from_name"].ToString(),
        Encoding.UTF8);

      if (mailto != null)
      {
        foreach (string to in mailto)
        {
          mail.To.Add(new MailAddress(to));
        }
      }

      if (mailcc != null)
      {

        foreach (string cc in mailcc)
        {
          mail.CC.Add(new MailAddress(cc));
        }
      }

      if (mailbcc != null)
      {
        foreach (string bcc in mailbcc)
        {
          mail.Bcc.Add(new MailAddress(bcc));
        }
      }


      mail.IsBodyHtml = true;
      mail.Subject = mailSubject;
      mail.BodyEncoding = Encoding.UTF8;

      StringWriter sw = new StringWriter();
      HtmlTextWriter hw = new HtmlTextWriter(sw);
      panel.RenderControl(hw);
      mail.Body = sw.ToString();
      //SmtpClient smtpClient = new SmtpClient("127.0.0.1", 25);//設定E-mail Server和port
      //從WEB.CONFIG讀SMTP CONFIG
      SmtpClient smtpClient = new SmtpClient();
      smtpClient.Send(mail);
    }
    catch (Exception ex)
    {
      throw ex;
    }
  }


}